home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d3 / rettig.arc / TRSOURCE.EXE / BASE10.C < prev    next >
C/C++ Source or Header  |  1990-10-22  |  2KB  |  68 lines

  1. /*********
  2. * Base10.C 
  3. * by Leonard Zerman
  4. * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
  5. *
  6. * Syntax: BASE10( <expN>, <expC> )
  7. * Return: <expN> decimal value of <base number> 
  8. *         -1 if invalid character in <base number>
  9. * Note  : Maximum <base> is 36.
  10. *         Minimum <base> is 2.
  11. *         <base number> is character.
  12. *         The largest return value is that of a long int.
  13. *         Which is 2,147,483,647
  14. *         MAX <base 36> = "ZIK0ZJ"
  15. *         MAX <base 2 > = "11111111111111111111111111111111" (32 one's)
  16. *         If overflow occurs "**********" is returned.
  17. ********/
  18.  
  19. #include "trlib.h"
  20.  
  21. TRTYPE base10()                             /* declare the base10 function */
  22. {
  23.     static char lookup[] = {"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
  24.     char *instr;                         /* pointer to the passed string */
  25.     long i, j, k, base;
  26.     long result = 0L;               /* long int result for large numbers */
  27.      
  28.     if (PCOUNT == 2 && ISNUM(1) && ISCHAR(2))      /* check passed parms */
  29.     {                  
  30.         base  = _parnl(1);                                /* assign vars */
  31.         instr = _parc(2) ;
  32.     }
  33.     else
  34.     { 
  35.         _retnl( ERRORNEGL );                          /* return an error */
  36.         return;                                   /* return from program */
  37.     }
  38.  
  39.     if ( base > 36L || base < 2L )                 /* maximum base is 36 */
  40.     {                                               /* minimum base is 2 */
  41.         _retnl( ERRORNEGL );                     /* if first char is '0' */
  42.         return;                                          /* return error */
  43.     }
  44.  
  45.     j = 1L;
  46.  
  47.     for (i = (long)_tr_strlen( instr ); i > 0L; i--)
  48.     {      /* process all characters */
  49.        for (k = 0L; (k < base) && (toupper(instr[i -1L]) != lookup[k]); k++)
  50.              ;                         /* search for character in lookup */
  51.  
  52.           if ( k < base )            /* k = the position of character or */
  53.           {                                     /* k = base if not found */
  54.              result += ( k * j );
  55.              j *= base;               /* add (char position * base) */
  56.           }
  57.           else
  58.           {
  59.              _retnl( ERRORNEGL );                     /* return an error */
  60.              return;
  61.           }
  62.  
  63.     }
  64.  
  65.     _retnl( result );                                   /* return result */
  66. }
  67. /* eof */
  68.